package kt.smpp.pdu;

import ie.omk.smpp.message.DeliverSM;

import java.sql.Timestamp;

public class SMSRequest {
	static String PATTERN = "[,\\. ;:+/\\{\\}\\^\\&\\(\\)\"\\?\\<\\>\\~\\`\\!\\@\\#\\$\\%\\-=\\+']";
	private String senderNumber = "";
	private String serviceNumber = "";
	private String mobileOperator = "";
	private String commandCode = "INV";
	private String info = "";
	private Timestamp genDate;
	private Timestamp timeReceived;
	private String receiverNumber = "";
	private boolean tangBanBe = false;
	private int id = 0;
	private int requestId = 0;
	
	public SMSRequest() {
		
	}
	
	public SMSRequest(DeliverSM sm, String operator) {
		this.setSenderNumber(sm.getSource().getAddress());
		this.receiverNumber = sm.getSource().getAddress();
		this.serviceNumber = sm.getDestination().getAddress();
		this.setInfo(sm.getMessageText());
		this.mobileOperator = operator;
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Deprecated
	public int getRequestId() {
		return this.requestId;
	}

	@Deprecated
	public void setRequestId(int value) {
		this.requestId = value;
	}

	public Timestamp getTimeReceived() {
		return this.timeReceived;
	}

	public void setTimeReceived(Timestamp timeReceived) {
		this.timeReceived = timeReceived;
	}

	public SMSResponse getResponse() {
		SMSResponse response = new SMSResponse();
		response.setSenderNumber(getSenderNumber());
		response.setReceiverNumber(getReceiverNumber());
		response.setServiceNumber(getServiceNumber());
		response.setMobileOperator(getMobileOperator());
		response.setCommandCode(getCommandCode());
		
		response.setRequestId(getId());
		return response;
	}

	public boolean isTangBanBe() {
		return this.tangBanBe;
	}

	public void setTangBanBe(boolean tangBanBe) {
		this.tangBanBe = tangBanBe;
	}

	public String getReceiverNumber() {
		return this.receiverNumber;
	}

	public void setReceiverNumber(String receiverNumber) {
		this.receiverNumber = receiverNumber;
	}

	public String getCommandCode() {
		return this.commandCode;
	}

	public void setCommandCode(String commandCode) {
		this.commandCode = commandCode;
	}

	public Timestamp getGenDate() {
		return this.genDate;
	}

	public void setGenDate(Timestamp genDate) {
		this.genDate = genDate;
	}

	public String getInfo() {
		return this.info;
	}

	public void setInfo(String info) {
		if(info == null) {
			info = "";
		}
		this.info = info;
		String[] itms = info.split(PATTERN);
		if(itms.length > 0) {
			this.commandCode = itms[0].toUpperCase();
		}
	}

	public String getMobileOperator() {
		return this.mobileOperator;
	}

	public void setMobileOperator(String mobileOperator) {
		this.mobileOperator = mobileOperator;
	}

	public String getSenderNumber() {
		return this.senderNumber;
	}

	public void setSenderNumber(String senderNumber) {
		if(senderNumber == null) {
			return;
		}
		this.senderNumber = senderNumber;
	}
	
	public String getServiceNumber() {
		return this.serviceNumber;
	}

	public void setServiceNumber(String serviceNumber) {
		this.serviceNumber = serviceNumber;
	}

	@Override
	public String toString() {
		return "SMSRequest [commandCode=" + commandCode + ", genDate="
				+ genDate + ", id=" + id + ", info=" + info
				+ ", mobileOperator=" + mobileOperator + ", receiverNumber="
				+ receiverNumber + ", requestId=" + requestId
				+ ", senderNumber=" + senderNumber + ", serviceNumber="
				+ serviceNumber + ", tangBanBe=" + tangBanBe
				+ ", timeReceived=" + timeReceived + "]";
	}	
}
